package com.jielihaofeng.springboot.shorturl.controller;


import com.jielihaofeng.springboot.shorturl.common.Result;
import com.jielihaofeng.springboot.shorturl.constant.UrlConstants;
import com.jielihaofeng.springboot.shorturl.entity.ShortUrlVo;
import com.jielihaofeng.springboot.shorturl.service.ShortUrlService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.websocket.server.PathParam;
import java.io.IOException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Johnnie Wind
 * @since 2021-06-03
 */
@Controller
public class PageController {

    @Autowired
    ShortUrlService shortUrlService;

    @GetMapping("/")
    public String index() throws IOException {
        return "欢迎来到英雄联盟！";
    }

    @GetMapping("/{shortKey}")
    public String redirect(@PathVariable("shortKey") String shortKey) throws IOException {
        String url = shortUrlService.getUrl(shortKey);
        if (StringUtils.isBlank(url)) {
            return "redirect:/blank";
        }
        return "redirect:".concat(url);
    }

    @ResponseBody
    @GetMapping("/blank")
    public String blank() throws IOException {
        return "系统无此短链！";
    }

    @ResponseBody
    @GetMapping("/sb")
    public String sb() throws IOException {
        return "sb！";
    }
}

